package com.onekbase.demo.thead;

import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.Semaphore;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

public class SemaphoreDemo {
    private static Semaphore semaphore = new Semaphore(3);
    private static ThreadPoolExecutor executorService = new ThreadPoolExecutor(10,50,60, TimeUnit.SECONDS,new LinkedBlockingQueue<>(200));

    public static void main(String[] args) throws InterruptedException {
        for (;;){
            Thread.sleep(100);
            executorService.execute(() -> exec());
        }
    }

    static void exec() {
        try {
            semaphore.acquire();
            System.out.println("执行exec方法");
            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            semaphore.release();
        }
    }
}
